#!/usr/bin/python2.4

"""Tests J42 polars table."""

# pylint: disable-msg=C6304

import unittest
import j42polars
from matrix import Polar


class TestJ42OptimalSailingComputation(unittest.TestCase):
  """Tests class J42OptimalSailingComputation."""

  def RunTest(self, performance_model, true_wind, expect_speed):
    result_speed = performance_model.Index(true_wind)
    self.assertAlmostEqual(expect_speed, result_speed, 2)

  def testPerformanceModel(self):
    """Test that the performance_model look reasonable and accurate."""
    performance_model = j42polars.J42OptimalSailingComputation()
    self.RunTest(performance_model, Polar(0, 47), 0.0)

    self.RunTest(performance_model, Polar(6, 46), 0.0)  # In irons
    self.RunTest(performance_model, Polar(6, 47), 4.88)
    self.RunTest(performance_model, Polar(6, 60), 5.75)
    self.RunTest(performance_model, Polar(6, 90), 6.13)
    self.RunTest(performance_model, Polar(6, 120), 4.45)
    self.RunTest(performance_model, Polar(6, 173), 3.13)  # opt. run, jib + pole
    self.RunTest(performance_model, Polar(6, 174), 0.0)  # DDW, more or less
    self.RunTest(performance_model, Polar(6, 180), 0.0)  # DDW, more or less

    self.RunTest(performance_model, Polar(20, 39), 7.03)
    self.RunTest(performance_model, Polar(21, 39), 7.03)
    self.RunTest(performance_model, Polar(40, 39), 7.03)

    # Wind speed interpolation at mid points
    self.RunTest(performance_model, Polar(7, 173), (4.16 + 3.13) / 2)
    self.RunTest(performance_model, Polar(13, 173), (6.80 + 6.01) / 2)

    # direct tws, mid twa
    self.RunTest(performance_model,
                 Polar(6, (90 + 60) / 2), (6.13 + 5.75) / 2)

    self.RunTest(performance_model,
                 Polar(7, 75), ((6.13 + 5.75)/2 + (7.09 + 6.84)/2)/2)


def main():
  unittest.main()


if __name__ == '__main__':
  main()
